package com.smart_campus.sys.controller;

import com.smart_campus.campus.domain.CampusCarousel;
import com.smart_campus.campus.service.CampusCarouselService;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.upload.UploadService;
import com.smart_campus.sys.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 上传轮播图
 * @author Alfred
 * @time: 2020/6/24 8:14
 */
@RestController
@RequestMapping("files")
public class FileSubmitController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private CampusCarouselService carouselService;

    /**
     * 上传文件
     */
    @RequestMapping("uploadFiles")
    public Object uploadFiles(MultipartFile mf,Integer carouselType){
        System.out.println(carouselType);

        String path = this.uploadService.uploadImage(mf);
        Map<String,String> map=new HashMap<>();
        map.put("src",path);
        //更新数据库
        Subject subject= SecurityUtils.getSubject();
        ActiveUser activeUser= (ActiveUser) subject.getPrincipal();
        User user = activeUser.getUser();
        CampusCarousel carousel = new CampusCarousel();
        carousel.setUserId(user.getUserId());
        carousel.setCarouselType(carouselType);
        carousel.setImages(path);
        carouselService.insertSelective(carousel);
        return new DataGridView(map);
    }



    /**
     * 上传文件
     */
    @RequestMapping("uploadGoodsFile")
    public Object uploadGoodsFile(MultipartFile mf){

        String path = this.uploadService.uploadImage(mf);

        Map<String,String> map=new HashMap<>();
        map.put("src",path);
        return new DataGridView(map);
    }


}
